<?php

/**
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
if ($_REQUEST) {
    require_once(dirname(__FILE__) . '/../config/config.inc.php');

    if ($_POST['cpanel'] == 'createad') {
        $session = new Session('account');
        $cpfcnpj = preg_replace('/\D/s', '', $_POST['cpfcnpj_owner']);
        if (empty($cpfcnpj) || Util::is_cpfcnpj($cpfcnpj)) {
            $msg = 'SUCCESS';
            if ($id = Util::renavam_exists($session->id, $_POST['renavam'])) {
                if (!isset($_POST['id']) || $id != $_POST['id'])
                    $msg = 'RENAVAM_EXISTS';
            }
            echo $msg;
        }
    }
    elseif ($_REQUEST['cpanel'] == 'pay-ads') {
        header('Cache-Control: no-cache');
        header('Content-type: application/xml; charset="utf-8"', true);

        $session = new Session('account');

        $id = $_REQUEST['id'];

        if ($session->type == 'F') {
            $vehicle_ad = DbGet::customer_vehicle_ad($id);
            $data = DbGet::plan($vehicle_ad->id_plan);
            $data->identity = 'F' . $vehicle_ad->cv_id;
            $data->email_pagseguro = DbGet::miscellaneous('email_pagseguro');
        } elseif ($session->type == 'J') {
            $account = DbGet::enterprise($id);
            $data = DbGet::plan($account->id_plan);
            $data->identity = 'J' . $account->e_id;
            $data->email_pagseguro = DbGet::miscellaneous('email_pagseguro');
        }
        else
            $data = null;

        echo json_encode($data);
    }elseif ($_REQUEST['cpanel'] == 'pay-product') {
        header('Cache-Control: no-cache');
        header('Content-type: application/xml; charset="utf-8"', true);
        $session = new Session('account');
        $id = $_REQUEST['id'];
        if($session->type){
            $product = DbGet::products_status($id, $session->type);
            $data = DbGet::plan($product->p_id);
            $data->identity = $session->type .'P'. $product->c_id;
            $data->email_pagseguro = DbGet::miscellaneous('email_pagseguro');
        }else{
            $data = null;
        }
        echo json_encode($data);
    }
}